﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormAssigment.DAO;
using System.Data.SqlClient;
using WinFormAssigment.GUI;

namespace WinFormAssigment
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();            
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text = " Hi ! "+Query.permission + " : " + Query.nameUser;
            Query.permission = "";
            Query.nameUser = "";
            toolStrip2.Visible = false;
        }
        bool flag = false;
        private void button1_Click(object sender, EventArgs e)
        {
            
            if (!flag)
            {
                toolStrip2.Visible = true;
                flag = !flag;
            }
            else {
                toolStrip2.Visible = false;
                flag = !flag;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //SqlConnection sql = DatabaseInteraction.GetConnectionDB();
            //if (sql == null)
            //{
            //    MessageBox.Show(DatabaseInteraction.message + "====" + DatabaseInteraction.strConnect);
            //}
            //else {
            //    MessageBox.Show(DatabaseInteraction.message + "====" + DatabaseInteraction.strConnect);
            //}
            AttendanceDemo dialog = new AttendanceDemo();
            AddNewTabPage(dialog);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            StudentAttendance dialog = new StudentAttendance();
            AddNewTabPage(dialog);
        }
        private void GetAttendaceControl() {
            StudentAttendance dialog = new StudentAttendance();
            AddNewTabPage(dialog);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ClassManager dialog = new ClassManager();
            AddNewTabPage(dialog);
        }
        private void AddNewTabPage(Form dialog)
        {
            dialog.TopLevel = false;
            dialog.MdiParent = this;
            TabPage myPage = new TabPage();
            myPage.Text = dialog.Text.ToUpper();
            if (!IsSameTabpage(myPage))
            {
                myPage.Controls.Add(dialog);
                dialog.FormBorderStyle = FormBorderStyle.None;
                dialog.Dock = DockStyle.Fill;
                tabControl1.TabPages.Add(myPage);
                dialog.Visible = true;
                tabControl1.SelectedTab = myPage;
            }
        }
        //method check the same tabpage
        private bool IsSameTabpage(TabPage myTabpage) {
            foreach (TabPage tp in tabControl1.TabPages)
            {
                if (tp.Text.Contains(myTabpage.Text)) {
                    return true;
                }
            }
            return false;
        }
        //event dong tabpage
        private ContextMenuStrip GetCMSForTabControl() {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Items.Add("Close",null, new EventHandler(Item_Clicked));
            return cms;
        }
        private void Item_Clicked(object sender, EventArgs e) {
            TabPage current = tabControl1.SelectedTab;
            tabControl1.TabPages.Remove(current);
        }

        private void tabControl1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right) {
                tabControl1.ContextMenuStrip = GetCMSForTabControl();
            }
        }        
        //-------
        
    }
}
